<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
require "base_msg.php";
/**
 *生成随机数
 */
function GetRandStr($length)
{
    $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $len = strlen($str) - 1;
    $randstr = '';
    for ($i = 0; $i < $length; $i++) {
        $num = mt_rand(0, $len);
        $randstr .= $str[$num];
    }
    return $randstr;
}

function upload($checkData){
    // 获取表单上传文件 例如上传了001.jpg
    $file = request()->file('file');
    if(empty($file)){
        $msg['code']=0;
        return $msg;
    }
    // 移动到框架应用根目录/public/uploads/ 目录下
    $info = $file->validate($checkData)->move(ROOT_PATH . 'public' . DS . 'uploads');
    if($info){
        $a=$info->getSaveName();
        $imgp= str_replace("\\","/",$a);
        $imgpath='/uploads/'.$imgp;
        $msg['code']=1;
        $msg['path']=$imgpath;
        return $msg;
    }else{
        // 上传失败获取错误信息
        //  echo $file->getError();
        $msg['code']=2;
        return $msg;
    }
}
/***
 * POST或GET请求
 * @url 请求url
 * @data POST数据
 * @return
 **/
function httpRequest($url, $data = ""){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if(!empty($data)){  //判断是否为POST请求
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}